/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 08 - 25
 * Time: 11:00
 */
//牛客 删除有序链表中重复的元素-I
public class Solution4 {
    //法一 我的方法
    public ListNode deleteDuplicates (ListNode head) {
        if(head == null) return null;
        ListNode pre = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(pre.val == cur.val) {
                pre.next = cur.next;
                cur = cur.next;
            }else {
                pre = cur;
                cur = cur.next;
            }
        }
        return head;
    }

    //法二 官方方法
    public ListNode deleteDuplicates1 (ListNode head) {
        if(head == null) return null;
        ListNode cur = head;
        while (cur != null && cur.next != null) {//这个条件很重要，因为下面有cur.next.next
            if(cur.val == cur.next.val) {
                cur.next = cur.next.next;
            }else {
                cur = cur.next;
            }
        }
        return head;
    }
}
